var app = getApp();
Page({

  data: {
    info:[],
    choose:'',
    itemList: ['正常','报废'],
    status:'点击选择状态',
    userInfo: null,
    title:'',
    buttom:'',
    zhicheng:'',
    lid:'9999999',//返工记录id

  },

  onLoad: function (options) {
    var lid = options.id;
    console.log(lid)
    this.setData({ lid: lid});
    this.getlist(lid)
    
  },

  onShow: function () {
    if (!wx.getStorageSync('userInfo')) {
      app.error("请登录后再操作", function () {
        setTimeout(function () { wx.navigateBack({}) }, 2000);
      });
    }else{

      this.setData({ userInfo:wx.getStorageSync('userInfo')});

      this.setData({ title:wx.getStorageSync('title')});

      if(wx.getStorageSync('gerenziliao')){

        this.setData({ userinfo:wx.getStorageSync('gerenziliao')});
      }
    

  
    }
  },
  fgwork(){
    wx.redirectTo({
      url: '/page/my/gongdanjilu',
    })
  },

  changezt(){
    var that = this
    wx.showActionSheet({
      itemList: ['正常','报废'],
      success(res) {
        that.setData({ status: that.data.itemList[res.tapIndex]});
        that.setData({ choose: res.tapIndex+1});
     
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },

  formSubmit: function (event) {
    var that = this;
  
    if(that.data.buttom == '领料'){

      if (event.detail.value.num > 0) {
     
      }else{
        app.error('领料数量不能为空');
        return;
      }
       
   
      wx.request({
        url: app.apiUrl + '/api/data/fggetproduce',
        data: {
          id: that.data.lid,
          openid:wx.getStorageSync('openid'),
          groupid: that.data.info.groupid,
          count:event.detail.value.num,
           ban:wx.getStorageSync('banci')
        },
        method: 'POST',
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: res=> {
      
         if(res.data.code == 1){
          wx.showModal({
            title: '温馨提示',
            content: res.data.msg,
            showCancel: false
          });

          setTimeout(function () {
            wx.redirectTo({
              url: '/page/my/index',
            }) 
          }, 2000)

          return false
         }else{
          wx.showModal({
            title: '温馨提示',
            content: res.data.msg,
            showCancel: false
          });
         }
  
        }
      })

    }



    if(that.data.buttom == '提交'){
      var choose = that.data.choose

      if(!choose){
        app.error('请选择状态');
        return;
      }
      if(choose == 2){
        choose = 3;//报废
      }

      if(event.detail.value.num>0 && event.detail.value.num <= that.data.info.num){

      }else{
        app.error('请输入正确的数量');
        return;
      }

      // console.log(that.data.choose)

       if(that.data.choose == 1){
         if(event.detail.value.num != that.data.info.num){
          app.error('正常状态的数量为:'+that.data.info.num);
          return;
         }
       }


      wx.request({
        url: app.apiUrl + '/api/data/fgcomproduce',
        data: {
          id: that.data.lid,
          openid:wx.getStorageSync('openid'),
          groupid: that.data.info.groupid,
          count2:event.detail.value.num,
          type:that.data.choose,
          count:that.data.info.num,
          llid:that.data.info.llid,
           ban:wx.getStorageSync('banci')
        },
        method: 'POST',
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: res=> {
      
         if(res.data.code == 1){
          wx.showModal({
            title: '温馨提示',
            content: res.data.msg,
            showCancel: false
          });

          setTimeout(function () {
            wx.redirectTo({
              url: '/page/my/index',
            }) 
          }, 2000)

          return false
         }else{
          wx.showModal({
            title: '温馨提示',
            content: res.data.msg,
            showCancel: false
          });
         }
  
        }
      })


    }


  
  },

  getlist:function (lid){
    var that = this
    wx.request({
      url: app.apiUrl + '/api/data/getFinshedfg',
      data: {
        openid:wx.getStorageSync('openid'),
        id:lid
      },
      method: 'post',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      success: function (res) {
        if(res.data.code == 1){
          if(res.data.data.show == 0){
            //领料
            that.setData({ buttom: '领料'});

          }else{
            //提交
            that.setData({ buttom: '提交'});
          }
          that.setData({ info: res.data.data});
              
        }else{
          wx.showModal({
            title: '温馨提示',
               content: res.data.msg,
                 showCancel: false,
                 success:function(res){
                  wx.navigateTo({
                    url: '/page/my/index',
                  })
                 }

            });
        }
     
      }
    });
  },
  //上传头像
  uploadAvatar: function () {
    var that = this;
    wx.chooseImage({
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        var formData = app.globalData.config.upload.multipart;
        formData.token = app.globalData.userInfo.token;
        wx.uploadFile({
          url: app.globalData.config.upload.uploadurl,
          filePath: tempFilePaths[0],
          name: 'file',
          formData: formData,
          success: function (res) {
            var row = JSON.parse(res.data);
            if (row.code == 1) {
              app.success('头像上传成功');              
              that.setData({ ["userInfo.avatar"]: app.globalData.config.upload.cdnurl + row.data.url });
            }
          },
          error: function (e) {
            console.log(e);
          }
        });
      }, error: function () {
        console.log(e);
      }
    });
  }

})